package com.java.tcpsocket;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerThread extends Thread {
//属性
	private ServerSocket ssocket;
	//

	@Override
	public void run() {

		try {
			// 初始化
			ssocket = new ServerSocket(8811);
			while (true) {
				// 创建一个与客户端连接的socket对象
				Socket socket = ssocket.accept();
				/**
				 * 服务器接收消息
				 */
				BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream(), "utf-8"));
				String str1 = br.readLine();
				System.out.println("服务端接收消息：" + str1);
				/**
				 * 服务器发送消息
				 */
				BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "utf-8"));
				String str = "你好，我是服务器！";
				bw.write(str);
				System.out.println("服务端发送消息："+str);
				bw.newLine();
				bw.flush();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
